package com.atguigu.guli.service.edu.controller.api;

import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.edu.entity.*;
import com.atguigu.guli.service.edu.entity.vo.WebCourseQueryVo;
import com.atguigu.guli.service.edu.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lj on 2021/2/7.
 * @version 1.0
 */
//@CrossOrigin
@Api(description="课程")
@RestController
@RequestMapping("/api/edu/course")
public class ApiCourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Autowired
    private ChapterService chapterServicel;
    @Autowired
    private VideoService videoService;
    @ApiOperation("课程列表")
    @GetMapping("list")
    public R list(
            @ApiParam(value = "查询对象", required = false)
                    WebCourseQueryVo webCourseQueryVo) {
        List<Course> courseList = courseService.webSelectList(webCourseQueryVo);
        return R.ok().data("courseList", courseList);
    }

    @ApiOperation("根据id查询课程信息")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "课程信息id", required = true) @PathVariable String id) {
        final Course course = courseService.getById(id);
        if (course != null) {
        //老师name 一级专业 二级专业
        final Teacher teacher = teacherService.getById(course.getTeacherId());
        course.setTeacherName(teacher.getName());
        course.setAvatar(teacher.getAvatar());
        course.setSubjectParentTitle(subjectService.getById(course.getSubjectParentId()).getTitle());
        course.setSubjectTitle(subjectService.getById(course.getSubjectId()).getTitle());

        final List<CourseDescription> courseDescriptions = courseDescriptionService.list(new QueryWrapper<CourseDescription>().eq("course_id", id));
        if(!CollectionUtils.isEmpty(courseDescriptions)){
            course.setDescription(courseDescriptions.get(0).getDescription());
        }
        //封装章节视频vo
        final List<Chapter> chapterList = chapterServicel.list(new QueryWrapper<Chapter>().eq("course_id", id));
        chapterList.forEach(chapter -> {
            final List<Video> videos = videoService.list(new QueryWrapper<Video>().eq("course_id", id).eq("chapter_id", chapter.getId()));
            chapter.setChildren(videos);
        });
        //更新课程浏览数
        final Course updateCourse = new Course();
        updateCourse.setId(course.getId());
        updateCourse.setViewCount(course.getViewCount()+1);
        courseService.updateById(updateCourse);
        return R.ok().data("course", course).data("chapterVoList",chapterList).message("获取课程信息成功！");
    }
        return R.error().message("获取课程信息失败！");
    }

    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("inner/get-course-dto/{courseId}")
    public CourseDto getCourseDtoById(
    @ApiParam(value = "课程ID", required = true)
        @PathVariable String courseId){
        CourseDto courseDto = courseService.getCourseDtoById(courseId);
        return courseDto;
    }

    @ApiOperation("根据课程id更新课程的购买数量")
    @GetMapping("inner/update-buy-count/{id}")
    R updateBuyCountById( @ApiParam(value = "课程ID", required = true)
                          @PathVariable("id") String courseId){
        //得到课程
        final Course course = courseService.getById(courseId);
        if(course!=null){
            final Course updateCourse = new Course();
            updateCourse.setId(courseId);
            updateCourse.setBuyCount(course.getBuyCount()+1);
            //更新课程的购买数量
            final boolean res = courseService.updateById(updateCourse);
            if(res){
                return R.ok().message("更新课程成功！");
            }
        }
        return R.error().message("更新课程失败！");
    }
}
